package cbfg.http.extra

import java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.net.UnknownHostException
import java.security.KeyStore
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

class DefSSLSocketFactory @JvmOverloads constructor(keyStore: KeyStore? = null) :
    SSLSocketFactory() {

    val sslContext: SSLContext = SSLContext.getInstance("TLS")

    private val trustManager: TrustManager = object : X509TrustManager {
        @Throws(CertificateException::class)
        override fun checkClientTrusted(
            x509Certificates: Array<X509Certificate>,
            s: String
        ) {
        }

        @Throws(CertificateException::class)
        override fun checkServerTrusted(
            x509Certificates: Array<X509Certificate>,
            s: String
        ) {
        }

        override fun getAcceptedIssuers(): Array<X509Certificate?> {
            return arrayOfNulls(0)
        }
    }

    fun getTrustManager(): X509TrustManager {
        return trustManager as X509TrustManager
    }

    override fun getDefaultCipherSuites(): Array<String?> {
        return arrayOfNulls(0)
    }

    override fun getSupportedCipherSuites(): Array<String?> {
        return arrayOfNulls(0)
    }

    @Throws(IOException::class)
    override fun createSocket(): Socket {
        return sslContext.socketFactory.createSocket()
    }

    @Throws(IOException::class)
    override fun createSocket(
        socket: Socket,
        host: String,
        post: Int,
        autoClose: Boolean
    ): Socket {
        return sslContext.socketFactory.createSocket(socket, host, post, autoClose)
    }

    @Throws(IOException::class, UnknownHostException::class)
    override fun createSocket(s: String, i: Int): Socket? {
        return null
    }

    @Throws(IOException::class, UnknownHostException::class)
    override fun createSocket(
        s: String,
        i: Int,
        inetAddress: InetAddress,
        i1: Int
    ): Socket? {
        return null
    }

    @Throws(IOException::class)
    override fun createSocket(inetAddress: InetAddress, i: Int): Socket? {
        return null
    }

    @Throws(IOException::class)
    override fun createSocket(
        inetAddress: InetAddress,
        i: Int,
        inetAddress1: InetAddress,
        i1: Int
    ): Socket? {
        return null
    }

    init {
        sslContext.init(null, arrayOf(trustManager), null)
    }
}